﻿<template>
    <div id="app">
        <mt-header v-bind:title="title" style="background-color:#fda10e">
            <a slot="left" @click="back()">返回</a>
            <mt-button v-if="usertype==2" slot="right" @click="add()">添加</mt-button>

        </mt-header>



        <div class="mui-table-view" v-for="item in list" @click="go(item)">
            <li class="mui-table-view-cell mui-media">
                <a href="javascript:;">
                    <img class="mui-media-object mui-pull-left" v-if="item.imgurl!=''" :src="item.imgurl">
                    <img class="mui-media-object mui-pull-left" v-else src="../assets/images/icon_nav_08.png">
                    <div class="mui-media-body">
                        <p class="mui-ellipsis" style="font-size:14px;">{{item.title}}</p>
                        <p class="" style="font-size:12px;">班级: {{item.classname}}</p>
                        <p class="" style="font-size:12px;">完成时间: {{item.overtime}}</p>
                    </div>
                </a>
            </li>


        </div>

        <ul v-infinite-scroll="loadMore"
            infinite-scroll-disabled="busy"
            infinite-scroll-distance="10" class="clearfix">
            <div class="loading">{{loading}}</div>
        </ul>


    </div>

</template>

<script>
    import Bus from '../bus.js'
    export default {
        name: 'app',
        data() {
            return {
                title: '作业',
                page: 1,
                list: [],
                busy: true,
                loading: "加载中...",
                usertype: ""
            }
        },
        components: {

        },
        mounted() {
            window.history.pushState('forward', null, '#');
            window.history.forward(1);
            window.onpopstate = () => {
                Bus.$emit('val', "show");
                this.$router.go(-1);
            }
        },
        methods:
        {
            go: function (e) {
                Bus.$emit('val', "hide");
                this.$router.push({ name: 'workpage', params: { id: e.id } });
            },
            add: function () {
                Bus.$emit('val', "hide");
                this.$router.push({ name: 'workadd', params: {} });
            },
            loadMore() {
                this.$data.busy = true  //将无限滚动给禁用
                setTimeout(() => {  //发送请求有时间间隔第一个滚动时间结束后才发送第二个请求
                    this.$data.page = this.$data.page + 1;
                    this.load();
                }, 500);
            },
            load: function () {
                var studentid = window.localStorage.getItem("studentid");
                var teacherid = window.localStorage.getItem("teacherid");
                var page = this.$data.page;
                var _this = this;
                this.$api.post('wx/Index/getwork', {
                    "studentid": studentid, "page": page, "teacherid": teacherid
                }, response => {
                    if (response.status >= 200 && response.status < 300) {
                        console.log(response.data);
                        var data = response.data;
                        if (data.status == 200) {

                            if (data.data.length > 0) {
                                for (var i = 0; i < data.data.length; i++) {

                                    _this.$data.list.push(data.data[i]);
                                }


                                _this.$data.busy = false;
                            } else {

                                _this.$data.busy = true;
                                _this.$data.loading = "没有数据了";
                            }



                        }


                        console.log(_this.$data.list);

                        //请求成功，response为成功信息参数
                    } else {
                        console.log(response.message);
                        //请求失败，response为失败信息
                    }
                });

            },
            back: function () {
                Bus.$emit('val', "show")
                this.$router.replace({ name: 'home', params: {} });
            }
        },
        created: function () {
            this.load();
            var usertype = window.localStorage.getItem("usertype");
            this.$data.usertype = usertype;
        }

    }
</script>

<style scoped>
    @import "../assets/css/mui.min.css";

    .loading {
        width: 100%;
        margin-bottom: 2px;
        text-align: center;
    }
</style>